﻿@model IEnumerable<Menu>
@{
    DBEntities db = new DBEntities();
    ViewBag.Title = "Index";
    var parentNode = new TreeNode();
    parentNode.ID = "";
    parentNode.Name = "所有菜单";
    parentNode.Open = true;
    parentNode.Children = new Service.MenuService(db).TreeNode(null);
}
@section viewAction{
    @Ajax.ActionLink(SharedResource.Create, "Edit", null, new CustomAjaxOptions() { UpdateTargetId = "menuDetails" }, new { @class = "ui-state-default button" })
    @Html.Partial("_Button_Search")
}
<table class="default">
    <tr>
        <td valign="top" style="width: 30%">
            <ul id="menuTrees" class="ztree">
            </ul>
        </td>
        <td valign="middle">
            <div id="loading" style="text-align: center">
                <img alt="" src="../../../../Content/images/loading.gif" /></div>
            <div id="menuDetails">
            </div>
        </td>
    </tr>
</table>
<script type="text/javascript">
var zNodes = @(Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(parentNode)));
var setting = {
    check: {enable: false},
    view: {
        addHoverDom: addHoverDom,
		removeHoverDom: removeHoverDom,
		selectedMulti: false,
        showIcon: true
    },
    edit: {
		enable: true,
        removeTitle: "删除",
		renameTitle: "编辑"
	},
    data: {
        key: {
            id: "ID",
			title: "name"
		}
    },
    callback:{
        beforeDrag: beforeDrag,
		beforeDrop: beforeDrop,
        beforeRemove: beforeRemove,
        onDrag: onDrag,
		onDrop: onDrop,
        onRemove: onRemove,
        onClick:loadData
    }
};



function addHoverDom(treeId, treeNode) {
	var sObj = $("#" + treeNode.tId + "_span");
	if ($("#addBtn_"+treeNode.ID).length>0) {
        return;
    }
	var addStr = "<button type='button' class='add' id='addBtn_" + treeNode.ID + "' title='新增' onfocus='this.blur();'></button>";
    sObj.append(addStr);
	//点击节点后的添加按钮加载新增页面
    var btn = $("#addBtn_"+treeNode.ID);
	if (btn)
    {
        btn.bind("click", function(){
            //新增
            $.get('@Url.Action("Edit")',{ParentItemID:treeNode.ID,menuID:'@Request.QueryString["menuID"]'},function(data){
                $("#menuDetails").html(data);
            });
            return false;
        })
            //点击节点后的编辑按钮加载编辑页面
    $("#addBtn_"+treeNode.ID).parent().next(".edit").unbind("click").bind("click",function(){
	    //修改
       // console.log(this)
        $.get('@Url.Action("Edit")',{id:treeNode.ID,menuID:'@Request.QueryString["menuID"]'} ,function(data){
            $("#menuDetails").html(data);
        });
        return false;
	});
     }

};
function removeHoverDom(treeId, treeNode) {
	$("#addBtn_"+treeNode.ID).unbind().remove();
}

function beforeRemove(treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("menuTrees");
    zTree.selectNode(treeNode);
    if(confirm("确认删除菜单 " + treeNode.Name + " 吗？"))
    {
        $.post('@Url.Action("Delete")',{id:treeNode.ID},function(data){
            return true;
        })
    }
    else{return false;}
}
function onRemove(e, treeId, treeNode) {
               $("#menuDetails").html("");
                showInfoMsg("删除成功");
}

//点击节点事件方法
function loadData(event, treeId, treeNode){
    $.get('@Url.Action("Edit")',{Id:treeNode.ID,menuID:'@Request.QueryString["menuID"]'} ,function(data){
        $("#menuDetails").html(data);
    });
}
//--拖动方法-------------------------------------------------------------
function beforeDrag(treeId, treeNodes) {
}
function beforeDrop(treeId, treeNodes, targetNode, moveType) {
    if(targetNode==null || targetNode.DropInner === false){
        return false;
    }
	return true;
}
function onDrag(event, treeId, treeNodes) {
	
}
function onDrop(event, treeId, treeNodes, targetNode, moveType) {
    if(null == treeNodes){
        return;
    }
    for (var i=0; i<treeNodes.length; i++) {
        $.get('@Url.Action("MoveTo")',{id:treeNodes[i].ID,targetId:targetNode.ID,type:moveType} ,function(data){
            if(data == "ok"){
                showInfoMsg("移动成功");
            }
            else{
                showInfoMsg("移动失败");
            }
        });
    }
}
$(document).ready(function () {
    $.fn.zTree.init($("#menuTrees"), setting, zNodes);
});
</script>
